package com.meishij.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.meishij.R;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

public class Wode_Zhuce_Activity extends AppCompatActivity implements View.OnClickListener {

    private EditText zhuce_code;
    private TextView phone;
    private TextView tv_enter;
    private TextView tv_time;

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 120:
                    int arg1 = msg.arg1;
                    tv_time.setText(arg1 + "s重新获取验证码");
                    Message msg1 = Message.obtain();
                    msg1.arg1 = --arg1;
                    msg1.what = 120;
                    handler.sendMessageDelayed(msg1, 1000);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wode__zhuce);
        zhuce_code = ((EditText) findViewById(R.id.tv_zhuce_code));
        phone = ((EditText) findViewById(R.id.tv_zhuce_phone));
        tv_enter = ((TextView) findViewById(R.id.bt_zhuce_enter));
        tv_time = ((TextView) findViewById(R.id.tv_zhuce_time));
        tv_enter.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_zhuce_enter:
                if (TextUtils.isEmpty(phone.getText().toString())) {
                    Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (TextUtils.isEmpty(zhuce_code.getText().toString())) {
                        BmobSMS.requestSMSCode("15033251584", " 您的验证码是`%smscode%`，有效期为`%ttl%`分钟。您正在使用`%appname%`的验证码。【比目科技】", new QueryListener<Integer>() {
                            @Override
                            public void done(Integer state, BmobException e) {
                                if (e == null) {
                                    Toast.makeText(Wode_Zhuce_Activity.this, "发送验证码成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(Wode_Zhuce_Activity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                        tv_enter.setText("注册");
                        Message message = Message.obtain();
                        message.what = 120;
                        message.arg1 = 59;
                        handler.sendMessageDelayed(message, 1000);
                    } else {
                        BmobSMS.verifySmsCode(phone.getText().toString(), zhuce_code.getText().toString(), new UpdateListener() {
                            @Override
                            public void done(BmobException e) {
                              /*  Intent intent = new Intent();
                                //把返回数据存入Intent
                                intent.putExtra("phone", phone.getText().toString());
                                setResult(RESULT_OK, intent);
                                finish();*/
                               if (e == null) {
                                    Toast.makeText(Wode_Zhuce_Activity.this, "验证成功", Toast.LENGTH_SHORT).show();
                                    //TODO返回手机号
                                    Intent intent = new Intent();
                                    //把返回数据存入Intent
                                    intent.putExtra("phone", phone.getText().toString());
                                    setResult(RESULT_OK, intent);
                                    finish();
                                } else {
                                    Toast.makeText(Wode_Zhuce_Activity.this, "验证码输入错误", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }
                break;
        }
    }
}
